package tcp

import (
	"alarm_service/common/client_conn"
	"alarm_service/common/logger"
	"alarm_service/internal/repo/command"
	"alarm_service/internal/repo/device"
	"errors"
	"net"
)

type HeartBeatCase struct {
	deviceRepo device.Repo
	command.Repo
}

func NewHeartBeatCase(dr device.Repo, cr command.Repo) *HeartBeatCase {
	s := &HeartBeatCase{
		deviceRepo: dr,
		Repo:       cr,
	}
	return s
}
func (s *HeartBeatCase) HeartBeat(packet []byte, conn net.Conn) error {
	deviceId := client_conn.Get(conn)
	if deviceId != 0 {
		err := s.deviceRepo.SetHeartBeat(deviceId)
		if err != nil {
			logger.Errorf("[HeartBeat] save HeartBeat: %v", err)
		}
	} else {
		logger.Infof("[HeartBeat] 找不上上线设备 %d", deviceId)
		return errors.New("[HeartBeat]找不上上线设备")
	}
	return nil
}
